package org.budo.dubbo.protocol.http.exception.remoting;

import lombok.Getter;
import lombok.Setter;

/**
 * @author lmw
 */
@Getter
@Setter
public class SignCheckException extends RemotingRuntimeException {
    private static final long serialVersionUID = 5218224100834795832L;

    private Fail fail;

    /**
     * 加一个属性，传递message信息，内置的message经过Dubbo调用序列化后丢失
     */
    private String info;

    public SignCheckException() {}

    public SignCheckException(String message, Fail fail) {
        super(message);

        this.setFail(fail);
        this.setInfo(message);
    }

    /**
     * Json反序列化时报错，set property error, fail, argument type mismatch
     */
    public void setFail(Object fail) {
        if (null == fail) {
            return;
        }

        Fail valueOf = Fail.valueOf(fail.toString());
        this.fail = valueOf;
    }

    public String toString() {
        return super.toString() + ", fail=" + this.getFail() + ", info=" + this.getInfo();
    }

    /**
     * 失败原因
     */
    public enum Fail {
        /**
         * 需要参数 appKey
         */
        APP_KEY_EMPTY,

        /**
         * 需要参数 time
         */
        TIME_EMPTY,

        /**
         * 服务器端为当前appKey配置的secret为空
         */
        APP_SECRET_EMPTY,

        /**
         * 客户端sign未传
         */
        SIGN_EMPTY,

        /**
         * sign 验证失败
         */
        SIGN_ERROR
    }
}